<?php
namespace Home\Controller;

use Home\Core\CommandSDK;

class UserListController extends BaseController
{
    /**
     * 检索用户列表
     */
    public function user_list()
    {
        $param=I('get.');
        $param['username']=I('username'); //企业名称
        $param['tel']=I('tel'); //手机号
        if($_SESSION['AreaId'] !== null){
            $param['areaid']=$_SESSION['AreaId'];
        }
        if(empty(I('pageid'))){
            $param['pageid']=1;
        }else{
            $param['pageid']=I('pageid');
        }
        if(empty(I('pagecount'))){
            $param['pagecount']=10;
        }else{
            $param['pagecount']=I('pagecount');
        }
        $param['role']=3;
        $sdk = new CommandSDK();
        $return = $this->http_request($sdk::GetClientList, $param, 'GET');
        $array = json_decode(json_encode($return), true);
        $pageid=$param['pageid'];
        $pagecount=$param['pagecount'];
        $totnum=$return->totnum;
        $pagenum=$totnum/$pagecount;
        $this->assign('pageid', $pageid);//页数
        $this->assign('pagecount', $pagecount);//每页显示条数
        $this->assign('pagenum', $pagenum);//总页数
        $this->assign('totnum', $totnum); //总条数
        $this->assign('param',$param);   // 搜索条件
        $this->assign('array', $array);
        // 获取当前登录用户的角色
        $cookie = base64_decode($_COOKIE['user_info']);
        parse_str($cookie,$userinfo);
        $this->assign('userrole',$userinfo['userrole']);
        $this->display();
    }

    /**
     * 获取用户信息
     */
    public function GetUserInfo()
    {
        // 测试上传
        $param = I('post.');
        $sdk = new CommandSDK();
        $return = $this->http_request($sdk::GetUserInfo, $param, 'GET');
        $array = json_decode(json_encode($return), true);
        $array['msg']=$sdk::error_code[$return->result];
        $this->ajaxReturn($array);
    }

    /**
     * 添加普通用户
     */
    public function AddUser_post()
    {
        $param = I('post.');
        unset($param['cpasswd']);
        $passwd=$param['passwd'];
        $param['passwd']=substr(md5("$passwd"),0,16);
        if($_SESSION['AreaId'] !== null){
            $param['areaid']=$_SESSION['AreaId'];
        }
        $param['role']=3;//角色
        $param['validDate']=0;
        $sdk = new CommandSDK();
        $return = $this->http_request($sdk::AddUser, $param, 'GET');
        $array = json_decode(json_encode($return), true);
        $array['msg']=$sdk::error_code[$return->result];
        $this->ajaxReturn($array);
    }



    /**
     * 修改用户
     */
    public function modifyUser_post()
    {
        $param = I('post.');
        $param['validDate']=0;
        $sdk = new CommandSDK();
        $return = $this->http_request($sdk::ModifyUser, $param, 'GET');
        $array = json_decode(json_encode($return), true);
        $array['msg']=$sdk::error_code[$return->result];
        $this->ajaxReturn($array);
    }
    /**
     * 删除用户
     */
    public function delUser_post()
    {
        $param = I('post.');
        $sdk = new CommandSDK();
        $return = $this->http_request($sdk::DelUser, $param, 'GET');
        $array = json_decode(json_encode($return), true);
        $array['msg']=$sdk::error_code[$return->result];
        $this->ajaxReturn($array);
    }

    /**
     * 获取区域列表
     */
    public function getArea_list()
    {
        $param = I('post.');
        $param['pageid']=1;
        $param['pagecount']=10000;
        $sdk = new CommandSDK();
        $return = $this->http_request($sdk::getAreaLists, $param, 'GET');
        $array = json_decode(json_encode($return), true);
        $array['msg']=$sdk::error_code[$return->result];
        $this->ajaxReturn($array);
    }

    /**
     * 获取单个用户视频资源权限
     */
    public function user_list_xq()
    {
        $param = I('get.');
        if(empty($param['pageid'])){
            $param['pageid']=1;
        }else{
            $param['pageid']=$param['pageid'];
        }
        if(empty($param['pagecount'])){
            $param['pagecount']=10;
        }else{
            $param['pagecount']=$param['pagecount'];
        }
        $sdk = new CommandSDK();
        $return = $this->http_request($sdk::GetUserVideoPermissions, $param, 'GET');
        $array = json_decode(json_encode($return), true);
        $ability='';
        foreach ($array['respri'] as $key => $value) {
            $abilityArr = [];
            if ($value['ability'] & 0x00000001) {
                $ability = '主码流,';
            }
            if ($value['ability'] & 0x00000002) {
                $ability .= "子码流,";
                $array['respri'][$key]['ability']=$ability;
            }
            if ($value['ability'] & 0x00000004) {
                $ability .= "云台,";
                $array['respri'][$key]['ability']=$ability;
            }
            if ($value['ability'] & 0x00000008) {
                $ability .= "语音,";
                $array['respri'][$key]['ability']=$ability;
            }
            if ($value['ability'] & 0x00000010) {
                $ability .= "对讲,";
                $array['respri'][$key]['ability']=$ability;
            }
            if ($value['ability'] & 0x00000020) {
                $ability .= "WIFI探针,";
                $array['respri'][$key]['ability']=$ability;
            }
            if ($value['ability'] & 0x00000040) {
                $ability .= "农业传感,";
                $array['respri'][$key]['ability']=$ability;
            }
            $ability='';
        }
        $pageid=$param['pageid'];
        $pagecount=$param['pagecount'];
        $accountiddst=$param['accountiddst'];
        $username=$param['username'];
        $tel=$param['tel'];
        $totnum=$return->totnum;
        $pagenum=$totnum/$pagecount;
        $this->assign('pageid', $pageid);//页数
        $this->assign('username', $username);//用户名
        $this->assign('tel', $tel);//用户名
        $this->assign('pagecount', $pagecount);//每页显示条数
        $this->assign('pagenum', $pagenum);//总页数
        $this->assign('totnum', $totnum); //总条数
        $this->assign('accountiddst', $accountiddst);//id
        $this->assign('param',$param);   // 搜索条件
        $this->assign('array', $array);
        $this->display();
    }
  


}